goog.provide('ee.block');

goog.require('lime.Sprite');
goog.require('lime.animation.Sequence');
goog.require('lime.animation.FadeTo');

this.posi = 0;
this.posj = 0;

/**
 * A : player A
 * B : player B
 * C : blank
 */
this.side = 'C' ;
this.level = '0';
this.die = false;
this.show = false;

ee.Block =  function(i,j,type){
    lime.Sprite.call(this);

    this.posi = i;
    this.posj = j;

    this.side= type.charAt(0);
    this.level= type.charAt(1);
    
    switch(this.side){
        case "A":
            //this.setFill(Math.floor(Math.random()*155+100),Math.floor(Math.random()*155+100),Math.floor(Math.random()*155+100))
        case "B":
            //this.setFill(Math.floor(Math.random()*155+100),Math.floor(Math.random()*155+100),Math.floor(Math.random()*155+100))
            this.setFill("assets/block.png");
            break;
        case "C":
            break;
    }
    this.setSize(100,100);
    this.setPosition(60+this.posi*100,60+this.posj*100);

    goog.events.listen(this,"touchend",function(){
        helloworld.removeArrow();
        if(this.side != "C"){
            if(!this.show){
                this.setChildIndex(0);
                this.setFill("assets/"+this.side+"/"+this.level+".png");
                this.runAction( new lime.animation.Sequence(
                        new lime.animation.FadeTo(0.1).setDuration(0.1),
                        new lime.animation.FadeTo(1.0).setDuration(0.2)));
                this.show = true;
            }else{
                if(!this.die){
                    var node_left    = helloworld.obtainBlockBy_ij(this.posi-1,this.posj);
                    var node_right   = helloworld.obtainBlockBy_ij(this.posi+1,this.posj);
                    var node_top     = helloworld.obtainBlockBy_ij(this.posi,this.posj-1);
                    var node_bottom  = helloworld.obtainBlockBy_ij(this.posi,this.posj+1);
                    if(this.ifcanmove(node_left)){
                        leftArrow.showArrow(this,node_left);
                    }
                    if(this.ifcanmove(node_right)){
                        rightArrow.showArrow(this,node_right);
                    }
                    if(this.ifcanmove(node_top)){
                        topArrow.showArrow(this,node_top);
                    }
                    if(this.ifcanmove(node_bottom)){
                        bottomArrow.showArrow(this,node_bottom);
                    }
                }
            }
        }
    });
};

goog.inherits(ee.Block,lime.Sprite);

ee.Block.prototype.ifcanmove = function(block){
    if(null == block) return false;
    if(block.side == "C"){
        return true;
    }
    if(block.show){
        if(block.side != this.side){
            if(block.die == true){
                return true;
            }
            if(this.level < block.level){
                return true;
            }
            if(this.level==9 && block.level==1){
                return true;
            }
        }else{
            if(block.die == true){
                return true;
            }
        }
    }
    return false;
};


ee.Block.prototype.id = "block"; 
goog.exportSymbol('ee.Block', ee.Block);
